<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>发表攻略</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
    <style>
        .uploader-list {
            margin-left: -15px;
        }

        .uploader-list .info {
            position: relative;
            margin-top: -25px;
            background-color: black;
            color: white;
            filter: alpha(Opacity=80);
            -moz-opacity: 0.5;
            opacity: 0.5;
            width: 100px;
            height: 25px;
            text-align: center;
            display: none;
        }

        .uploader-list .handle {
            position: relative;
            background-color: black;
            color: white;
            filter: alpha(Opacity=80);
            -moz-opacity: 0.5;
            opacity: 0.5;
            width: 100px;
            text-align: right;
            height: 18px;
            margin-bottom: -18px;
            display: none;
        }

        .uploader-list .handle span {
            margin-right: 5px;
        }

        .uploader-list .handle span:hover {
            cursor: pointer;
        }

        .uploader-list .file-iteme {
            margin: 12px 0 0 15px;
            padding: 1px;
            float: left;
        }
        textarea{
            height: 150px;
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">发表攻略</div>
                <div class="layui-card-body">
                    <form class="layui-form" action="" lay-filter="component-form-group">

                        <div class="layui-form-item">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">内容</label>
                            <div class="layui-input-block">
                                <textarea style="height: 250px;padding-top: 10px" id="content" name="content" placeholder="攻略内容" autocomplete="off" class="layui-input"></textarea>
                            </div>
                        </div>

                        <div class="layui-upload" style="margin-left: 32px">
                             <button type="button" class="layui-btn" id="test2" >上传活动图片</button>
                            <span>上传750px*360px的图片<a style="color: red">*请在提示全部上传成功后再点击提交，上传几张图片会有几次上传成功</a></span>
                            <input type="hidden" name="image" id="image" value="">
                            <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;width: 88%">
                                    预览图：
                                    <div class="layui-upload-list uploader-list" style="overflow: auto;" id="uploader-list" >

                                    </div>
                            </blockquote>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">允许精选</label>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="is_select" value="1" title="允许">
                                </div>
                            </div>

                        </div>
                        {if !empty($tour)}
                        <div class="layui-form-item">
                            <label class="layui-form-label">选择景点</label>
                            <div class="layui-input-block">
                                {foreach $tour as $key=>$v}
                                <input type="checkbox" name="view" title="{$v['tour_name']}" value="{$v['tour_id']}+{$v['tour_name']}" lay-verify="required" lay-filter="test">
                                {/foreach}
                            </div>
                        </div>
                        {/if}
                        <div class="layui-form-item layui-layout-admin">
                            <div class="layui-input-block">
                                <div class="layui-inline" style="left: 0;">
                                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="addsubmit">立即发布</button>
                                    <a class="layui-btn layui-btn-primary prex" data-type="prex">返回</a>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>

<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','set','upload'], function(){
        $ = layui.jquery;
        var  admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,form = layui.form
            ,upload=layui.upload;

        form.render(null,'component-form-group');
        var arr_image = new Array();

        var uploadInst = upload.render({
            elem: '#test2'
            ,url: "{:url('Common/upload')}"
            ,multiple: true
            ,before: function(obj){
                obj.preview(function (index, file, result) {

                    //上传完毕
                    $('#uploader-list').append(
                        '<div id="id" class="file-iteme">' +
                        '<img style="width: 140px;height: 100px;" src=' + result + '>' +
                        '</div>'
                    );
                });
            }
            ,done: function(res){
                //如果上传失败
                if (res.code == 0) {
                    return layer.msg(res.msg);
                }

                //上传成功
                if (res.code == 1) {
                    arr_image.push(res.data);
                    $('#image').val(res.data);
                    return layer.msg(res.msg);
                }
            }
        });

        //添加提交
        form.on('submit(addsubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            //获取chekbox
            var arr = new Array();
            $("input:checkbox[name='view']:checked").each(function(i){
                arr[i] = $(this).val();
            });

            data.field.view = arr.join(",");//将数组合并成字符串
            data.field.image = arr_image.join(",");
            data.field.content = $('#content').val();
            $.ajax({
                url: "{:url('Method/addMethod')}",
                type: 'post',
                dataType: 'json',
                data: {data:data.field},
                success: function(data){
                    if (data.code === 0) {
                        layer.msg('添加成功',{icon:1,time:2000},function(){
                            window.location.href = "{:url('Method/methodlist')}";
                        });
                    }else if(data.code === 2){
                        layer.close(loading);
                        layer.msg('添加失败',{icon:2,time:1000})
                    }else if(data.code === 1){
                        layer.close(loading);
                        layer.msg(data.msg,{icon:2,time:1000})
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        form.on('checkbox(test)', function(data){
            data.elem.checked;
            console.log(data.elem.attributes['title'].value); //得到checkbox原始DOM对象

        });


        var $ = layui.$, active = {
            prex:function(){
                layer.confirm('确认取消本次编辑并返回吗？',function(){
                    window.location.href="{:url('Method/methodlist')}";
                });
            }
        };

        $('.prex').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>


</body>
</html>